﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.Numerics;
using System.Reflection;
using System.Reflection.Metadata;

namespace hnrzxt.Controllers
{
    public class TreeDataController : Controller
    {
        private readonly ApplicationDbContext _db;
        public TreeDataController(ApplicationDbContext db)
        {
            _db = db;
        }
        public IActionResult GetOrgTreeData()
        {
            return new JsonResult(getOrgTree("0")); //0从华南集团开始，4从华南集团下级开始
        }


        public List<sys_area> getthesheng()
        {


            var x = "0";
            List<sys_area> li = _db.sys_area.Where(p => p.F_ParentId == x).ToList();





            return li;
        }



        public List<sys_area> gettheshithree(string f_id)
        {

            List<sys_area> li = _db.sys_area.Where(p => p.F_ParentId == f_id).ToList();
            return li;


        }
        public List<sys_area> gettheshi(string f_id)

        {




            if (f_id != "120000000000" && f_id != "500000000000" && f_id != "110000000000" && f_id != "310000000000")
            {

                List<sys_area> li = _db.sys_area.Where(p => p.F_ParentId == f_id).ToList();
                return li;

            }

            else {


                var  li = _db.sys_area.Where(p => p.F_ParentId == f_id).ToList();
                string itemid = li[0].F_Id;
                List<sys_area> itemli = _db.sys_area.Where(p => p.F_ParentId == itemid).ToList();

                return itemli;

            }



                  

            

        }


        public List<sys_area> getthexian(string f_id)

        {


            List<sys_area> li = _db.sys_area.Where(p => p.F_ParentId == f_id).ToList();

            return li;

        }

        public IActionResult GetOrgTreeDatanew()
        {
            return new JsonResult(getOrgTreenew("0")); //0从华南集团开始，4从华南集团下级开始
        }


        private List<Dictionary<string, object>> getOrgTreenew(string ParentOrgId)
        {
            List<Org> orglist = _db.Orgs.Where(p => p.ParentOrgId == ParentOrgId).ToList();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            if (orglist.Count <= 0)
            {
                return null;
            }

            for (int i = 0; i < orglist.Count; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map.Add("id", orglist[i].OrgId);
                map.Add("title", orglist[i].OrgName);
                //map.Add("name", orglist[i].OrgName);
                //map.Add("dataid", orglist[i].OrgId);


                List<Dictionary<string, object>> listchildren = new List<Dictionary<string, object>>();
                listchildren = getOrgTreenew(orglist[i].OrgId.ToString());
                if (listchildren != null)
                {
                    map.Add("child", listchildren);
                }
                list.Add(map);
            }
            return list;
        }

        private List<Dictionary<string, object>> getOrgTree(string ParentOrgId)
        {
            List<Org> orglist = _db.Orgs.Where(p => p.ParentOrgId == ParentOrgId).ToList();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            if (orglist.Count <= 0)
            {
                return null;
            }

            for (int i = 0; i < orglist.Count; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map.Add("id", orglist[i].OrgId);
                map.Add("title", orglist[i].OrgName);
                map.Add("name", orglist[i].OrgName);
                map.Add("dataid", orglist[i].OrgId);
                map.Add("url", orglist[i].WebUrl);


                List<Dictionary<string, object>> listchildren = new List<Dictionary<string, object>>();
                listchildren = getOrgTree(orglist[i].OrgId.ToString());
                if (listchildren != null)
                {
                    map.Add("children", listchildren);
                }
                list.Add(map);
            }
            return list;
        }

        public string GetOrgInfo(string OrgId)
        {
            //从数据库获取菜单数据
            List<Org> orglist = _db.Orgs.Where(p => p.OrgId == int.Parse(OrgId)).ToList();
            if (orglist.Count < 1)
                return OrgId;
            else
                return orglist[0].OrgName;
        }

        public IActionResult GetDingDeptData()
        {
            return new JsonResult(GetDingDeptTree(1)); //
        }

        private List<Dictionary<string, object>> GetDingDeptTree(int ParentOrgId)
        {
            List<DingOrga> DingDeptlist = _db.DingOrga.Where(p => p.DingParentOrgId == ParentOrgId).ToList();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            if (DingDeptlist.Count <= 0)
            {
                return null;
            }

            for (int i = 0; i < DingDeptlist.Count; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map.Add("id", DingDeptlist[i].DingID);
                map.Add("title", DingDeptlist[i].DingOrgName);
                //map.Add("name", orglist[i].OrgName);
                //map.Add("dataid", orglist[i].OrgId);


                List<Dictionary<string, object>> listchildren = new List<Dictionary<string, object>>();
                listchildren = GetDingDeptTree(DingDeptlist[i].DingID);
                if (listchildren != null)
                {
                    map.Add("child", listchildren);
                }
                list.Add(map);
            }
            return list;
        }


        //根据用户权限加载组织列表
        public IActionResult GetUserOrgTreeData(string UserId)
        {
            return new JsonResult(getUserOrgTree("0", UserId));
        }

        private List<Dictionary<string, object>> getUserOrgTree(string ParentOrgId, string UserId)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            List<User_Org> userorglist = _db.User_Orgs.Where(p => p.UserId == UserId).ToList();
            if (userorglist.Count < 1)
            {
                return null;
            }

            List<Org> orglist = _db.Orgs.Where(p => p.ParentOrgId == ParentOrgId).ToList();
            if (orglist.Count <= 0)
            {
                return null;
            }

            for (int i = 0; i < orglist.Count; i++)
            {
                for (int j = 0; j < userorglist.Count; j++)
                {
                    if (orglist[i].OrgId.ToString() != userorglist[j].OrgId)
                    {
                        continue;
                    }
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map.Add("id", orglist[i].OrgId);
                    map.Add("title", orglist[i].OrgName);
                    map.Add("name", orglist[i].OrgName);
                    map.Add("dataid", orglist[i].OrgId);
                    map.Add("url", orglist[i].WebUrl);

                    List<Dictionary<string, object>> listchildren = new List<Dictionary<string, object>>();
                    listchildren = getUserOrgTree(orglist[i].OrgId.ToString(), UserId);
                    if (listchildren != null)
                    {
                        map.Add("children", listchildren);
                    }
                    list.Add(map);


                }


            }
            return list;


        }

        //树形控件专用
        public IActionResult GetOrgTreeDataParams()
        {
            return new JsonResult(getOrgTreeParams("0"));
        }

        //递归获取子节点
        private List<Dictionary<string, object>> getOrgTreeParams(string ParentOrgId)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            //从数据库获取菜单数据
            List<Org> orglist = _db.Orgs.Where(p => p.ParentOrgId == ParentOrgId).OrderBy(p => p.Sort).ToList();

            if (orglist == null | orglist.Count <= 0)
            {
                return null;
            }
            for (int i = 0; i < orglist.Count; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map.Add("id", orglist[i].id);//节点唯一索引值，用于对指定节点进行各类操作String/Number
                map.Add("title", orglist[i].OrgName);//节点标题String
                map.Add("field", "");//节点字段名
                map.Add("href", "");//点击节点弹出新窗口对应的 url。需开启 isJump 参数String
                map.Add("spread", true);//节点是否初始展开，默认 false Boolean
                map.Add("checked", false);//节点是否初始为选中状态（如果开启复选框的话），默认 false Boolean
                map.Add("disabled", false);//节点是否为禁用状态。默认 false Boolean

                //扩展组织树
                map.Add("OrgId", orglist[i].OrgId);
                map.Add("name", orglist[i].OrgName);
                map.Add("url", orglist[i].WebUrl);
                map.Add("icon", orglist[i].Icon);
                map.Add("PId", orglist[i].ParentOrgId);
                map.Add("remark", orglist[i].Remark);
                map.Add("deptor", orglist[i].Deptor);
                map.Add("StopFlag", orglist[i].StopFlag);

                List<Dictionary<string, object>> listchildren = new List<Dictionary<string, object>>();
                listchildren = getOrgTreeParams(orglist[i].OrgId.ToString());
                if (listchildren != null)
                {
                    map.Add("children", listchildren);//子节点。支持设定选项同父节点 Array
                }
                list.Add(map);
            }
            return list;

        }

    }
}
